public class TestHeap {
    public int[] elem;
    public int usedSize;
    //默认数组大小为10
    public TestHeap() {
        this.elem = new int[10];
    }
    //提供方法传入数组
    public void initElem(int[] array) {
        for (int i = 0; i < array.length; i++) {
            elem[i] = array[i];
            usedSize++;
        }
    }
    //创建最大堆(向下调整)
    public  void creatBigHeap() {
        for(int parent = (usedSize-1-1)/2;parent >=0;parent--) {
            siftDown(parent,usedSize);//调整
        }

    }
    private void siftDown(int parent,int end) {
        int child = 2*parent+1;
        while (child < end) {
            if(child+1<end && elem[child]  < elem[child+1]) {
                child++;
            }
            if(elem[child] > elem[parent]) {
                //交换
                swap(child,parent);
                parent = child;
                child = 2*parent+1;
            }
        }
    }
    private void swap(int i,int j) {
        int tmp = elem[i];
        elem[i] = elem[j];
        elem[j] = tmp;
    }
}
